package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbAssetMapper;
import com.bwie.mapper.TbBankMapper;
import com.bwie.mapper.TbSroowMapper;
import com.bwie.pojo.TbAsset;
import com.bwie.pojo.TbBank;
import com.bwie.pojo.TbSroow;
import com.bwie.pojo.TbUser;
import com.bwie.service.TbUserService;
import com.bwie.mapper.TbUserMapper;
import com.bwie.utils.R;
import com.bwie.vo.LoginInfoVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.impl.crypto.JwtSigner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author wx200
 * @description 针对表【tb_user】的数据库操作Service实现
 * @createDate 2025-07-03 09:25:43
 */
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser>
        implements TbUserService {
    @Autowired
    TbUserMapper tbUserMapper;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    TbAssetMapper tbAssetMapper;

    @Autowired
    TbBankMapper tbBankMapper;

    @Autowired
    TbSroowMapper tbSroowMapper;

    @Override
    public R login(LoginInfoVo loginInfoVo) {
        LambdaQueryWrapper<TbUser> eq = new QueryWrapper<TbUser>().lambda().eq(TbUser::getPhone, loginInfoVo.getPhone());
        TbUser tbUser = tbUserMapper.selectOne(eq);
        if (tbUser == null) {
            return R.ERROR(500, "当前用户不存在");
        }
        if (!tbUser.getPassword().equals(loginInfoVo.getPassword())) {
            return R.ERROR(500, "当前密码不正确");
        }
        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "123456").claim("userId", tbUser.getId()).compact();
        stringRedisTemplate.opsForValue().set("token:" + tbUser.getId(), token,5,TimeUnit.MINUTES);
        tbUser.setToken(token);
        return R.OK(tbUser);
    }

    @Override
    public R assetList(Integer userId) {
        List<TbAsset> tbAssetList = tbUserMapper.assetList(userId);
        return R.OK(tbAssetList);
    }


    @Override
    public R getJiekuan(Integer userId) {
        List<TbSroow> tbSroows = tbSroowMapper.getJieKuan(userId);
        return R.OK(tbSroows);
    }

    @Override
    public R getSroow(TbSroow tbSroow) {
//        new QueryWrapper<TbSroow>().lambda().eq(TbSroow::getUserId)
        tbSroowMapper.updateById(tbSroow);
        return R.OK();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R getBrand(TbBank tbBank) {
        LambdaQueryWrapper<TbBank> eq = new QueryWrapper<TbBank>().lambda().eq(TbBank::getBank, tbBank.getBank());
        TbBank tbBank1 = tbBankMapper.selectOne(eq);
        tbBank.setBalance(tbBank1.getBalance());
//   提现
        if (tbBank.getStatusId() == 1) {
            if (tbBank.getShoppingMoeny() > 1000) {
                return R.ERROR(500, "当前提现金额超过存钱宝金额");
            } else {

                /**
                 * x修改当前存钱宝数据
                 */
                LambdaQueryWrapper<TbAsset> eq1 = new QueryWrapper<TbAsset>().lambda().eq(TbAsset::getUserId, tbBank.getUserId());
                TbAsset tbAsset = tbAssetMapper.selectOne(eq1);
                tbAsset.setTotal(tbAsset.getTotal() - tbBank.getShoppingMoeny());
                tbAssetMapper.updateById(tbAsset);
                return R.OK();
            }
        }

//        充值
        if (tbBank.getStatusId() == 2) {
            if (tbBank.getShoppingMoeny() > tbBank.getBalance()) {
                return R.ERROR(500, "当前银行卡余额不足充值失败");
            } else {
/**
 * 修改余额宝
 */
                LambdaQueryWrapper<TbAsset> eq1 = new QueryWrapper<TbAsset>().lambda().eq(TbAsset::getUserId, tbBank.getUserId());
                TbAsset tbAsset = tbAssetMapper.selectOne(eq1);
                tbAsset.setTotal(tbAsset.getTotal() + tbBank.getShoppingMoeny());
                tbAssetMapper.updateById(tbAsset);
                return R.OK();
            }
        }
        return R.OK();
    }

}




